<?php
class StaticCache
{
	private $config;
	private $config_s;
	
	function __construct()
	{
		$this->config = array();
		$this->config_s = $this->get_conf();
		$this->config['/news/content.php'] = array('catid','id','status');
	}
	
	private function get_conf()
	{
		$conf = array();
		if ($handle = opendir(CACHE_URL)) {
		    while (false !== ($file = readdir($handle))) {
		        if($file =='.' || $file=='..')
		        {
		        	continue;
		        }
		        if(is_file(CACHE_URL.$file) && file_exists(CACHE_URL.$file))
		        {
		        	$conf = array_merge($conf,file(CACHE_URL.$file));
		        }
		    }
		    closedir($handle);
		    $list = array();
		    foreach ($conf as $v)
		    {
		    	$tmp = explode(",",$v);
		    	if(sizeof($tmp) ==2)
		    	{
		    		$list[$tmp[0]] = $tmp[1];
		    	}
		    }
		    
		    return $list;
		}
		else 
		{
			return false;
		}
	}
	
	/**
	 * 生成静态地址
	 *
	 * @param unknown_type $url
	 * @return unknown
	 */
	function make_url($url)
	{
		if(isset($this->config_s[$url]))
		{
			return $this->config_s[$url];
		}
		$config = $this->config;
		
		$exp = parse_url($url);
		$key = $exp['path'];
		if(!isset($config[$key]))
		{
			return $url;
		}
		$params = array();
		$tmp_part = array();
		if(isset($exp['query']))
		{
			$tmp = explode('&',str_replace('&amp;','&',$exp['query']));
			if(is_array($tmp))
			{
				foreach ($tmp as $v)
				{
					$t = explode('=',$v);
					if(sizeof($t)==2)
					{
						$params[$t[0]] = $t[1];
					}
				}
			}
			
			$part = $config[$key];
			
			if(is_array($part))
			{
				foreach ($part as $v)
				{
					$tmp_part[$v] = isset($params[$v]) ? $params[$v] : '0';
				}
			}
		}
		if(sizeof($tmp_part) > 0)
		{
			$url = str_replace('.php','',$key).'_'.implode('_',$tmp_part).'.html';
		}
		else 
		{
			$url = str_replace('.php','.html',$key);
		}
		return $url;
	}
	
	/**
	 * 根据静态地址获得url
	 *
	 * @param unknown_type $url
	 * @return unknown
	 */
	function get_url($url)
	{
		$config_s = array_flip($this->config_s);
		if(isset($config_s[$url]))
		{
			return $config_s[$url];
		}
		$file = basename($url);
		$path = str_replace($file,'',$url);
		
		$params_str = stristr($file,'_');
		$php = str_replace($params_str,'',$file).'.php';
		if(!isset($this->config[$path.$php]))
		{
			return $url;
		}
		$params_conf = $this->config[$path.$php];
		$params_arr = explode('_',str_replace('.html','',$params_str));
		
		$params =array();
		$query = '';
		if(is_array($params_arr))
		{
			$i=0;
			foreach ($params_arr as $v)
			{
				if(empty($v) || !isset($params_conf[$i]))
				{
					continue;
				}
				$params[$params_conf[$i]] = $params_conf[$i].'='.$v;
				$i++;
			}
			if($i>0)
			{
				$query = '?'.implode('&',$params);
			}
		}
		$url = $path.$php.$query;
		return $url;
	}
	
	/**
	 * 访问指定url
	 *
	 * @param unknown_type $url
	 * @return unknown
	 */
	function put_get($url) {
		$url = 'http://'.$_SERVER['HTTP_HOST'] . (APP_PATH!='/' ? substr(APP_PATH,0,-1) : '/') .$url; 
		return file_get_contents($url);
	    
	}
	
	
}
?>